* Firm organization with multiple establishments
* Appendix A: Tables A.2-A.14

clear all 
set matsize 2000
set more off

capture log close
log using log/26_cmrh-p_Replication_firm.log, replace

********************************************************************************
********************************************************************************
* Use individual-level dataset

use data/CMRH_2000-2010_p_layer-ind.dta, clear

*table A.2: distribution by layer
*****************************

tabstat tentgelt, by(layer) s(n mean sd p5 p10 q p90 p95)
tabstat d_educ, by(layer) s(n mean sd p5 p10 q p90 p95)
//	# obs.:
tab layer, missing

// Run stats without retail
gen w08_2 = int(w08_5 / 1000)
tabstat tentgelt if w08_2 != 47, by(layer) s(n mean sd p5 p10 q p90 p95)
tabstat d_educ if w08_2 != 47, by(layer) s(n mean sd p5 p10 q p90 p95)

********************************************************************************
********************************************************************************
* Use firm-level dataset

use data/CMRH_2000-2010_p_layer-firm.dta, clear

*** DATASET NEEDS TO VARY AT FIRM-YEAR LEVEL
duplicates report untid jahr

*table A.3: statistics by layers
******************************
table count_mgmt_unt, c(n empl_unt mean empl_unt mean avg_wage median avg_wage)

*table A.4: fraction of firms with correct ordering
***************************************************
tabulate correctlyr count_mgmt_unt, col nofreq
tabulate correctlyr count_mgmt_unt [aweight=empl_unt], col nofreq
//	# obs
tabulate correctlyr count_mgmt_unt
tabulate correctlyr count_mgmt_unt [aweight=empl_unt]
******************** end table 4

*Table A.5: # employees are hierarchical
**************************************

gen h01 = (empl_lyr0>=empl_lyr1 & empl_lyr0~=.)
gen h12 = (empl_lyr1>=empl_lyr2 & empl_lyr1~=.)
gen h23 = (empl_lyr2>=empl_lyr3 & empl_lyr2~=.)

*	fraction of firms satisfying hierarchy
*	In each cell:
* 	- first element: number of obs in cell
* 	- second element: fraction of firms with 1

*	data for table A.5
tabstat h01 h12 h23  if count_mgmt_unt>0, by(count_mgmt_unt) format(%12.3f) statistics(N mean)

count if h01 == 1 & h12 == 1 & count_mgmt_unt == 2
count if h01 == 1 & h12 == 1 & h23 == 1 & count_mgmt_unt == 3

drop  h01- h23
******************** end tables A.5



*Table A.6: wages are hierarchical
****************************************
*
gen h01 = (avg_wage_lyr0<=avg_wage_lyr1 & avg_wage_lyr1~=.)
gen h12 = (avg_wage_lyr1<=avg_wage_lyr2 & avg_wage_lyr2~=.)
gen h23 = (avg_wage_lyr2<=avg_wage_lyr3 & avg_wage_lyr3~=.)

*	fraction of firms satisfying hierarchy
*	In each cell:
* 	- first element: number of obs in cell
* 	- second element: fraction of firms with 1

*	data for table A.6
tabstat h01 h12 h23  if count_mgmt_unt>0, by(count_mgmt_unt) format(%12.3f) statistics(N mean)

count if h01 == 1 & h12 == 1 & count_mgmt_unt == 2
count if h01 == 1 & h12 == 1 & h23 == 1 & count_mgmt_unt == 3
******************** end tables A-6


* table A.7: changes in firm-level outcomes by reorganization
***************************************************************
*generating changes
foreach v in dlog_empl_unt dlog_norm_empl dlog_avg_wage dlog_notop  {
	
	display "---------- `v' "

	display "---------------- all"
	reg `v' if jahr<2010 , r
	estimates store a_`v'
	
	display "---------------- stayers"
	reg `v' if jahr<2010  & count_mgmt_unt==lnext, r
	estimates store s_`v'
	
	display "---------------- decrease"
	reg `v' if jahr<2010 & count_mgmt_unt>lnext, r
	estimates store d_`v'
	
	display "---------------- increase"
	reg `v' if jahr<2010  & count_mgmt_unt<lnext, r 
	estimates store i_`v'
	
	
	gen `v'_detr=.
	forvalues y=1998/2009 {
		sum `v' if jahr==`y'
		replace `v'_detr=`v'-r(mean) if jahr==`y'
	}

	display "---------------- all"
	reg `v'_detr if jahr<2010 , r
	estimates store ad_`v'
	
	display "---------------- stayers"
	reg `v'_detr if jahr<2010  & count_mgmt_unt==lnext, r
	estimates store sd_`v'
	
	display "---------------- decrease"
	reg `v'_detr if jahr<2010 & count_mgmt_unt>lnext, r
	estimates store dd_`v'
	
	display "---------------- increase"
	reg `v'_detr if jahr<2010  & count_mgmt_unt<lnext, r 
	estimates store id_`v'
	
	
	drop `v'_detr
}
*** results for table 7: main body
foreach v in dlog_empl_unt dlog_norm_empl dlog_avg_wage dlog_notop  {

	***** changes not detrended
	display "---------- `v' "
	estimates table a_`v'  i_`v'  s_`v' d_`v', star(0.1 0.05 0.01) stats(N) style(column) b(%9.3f)

	***** changes, detrended
	display "---------- `v', detrended "
	estimates table  ad_`v'  id_`v'  sd_`v' dd_`v' , star(0.1 0.05 0.01) stats(N) style(column) b(%9.3f)
}
estimates drop _all
******************** end table 7


*tables A.8, A.9, A.10
*****************************************************************************
*NON SWITCHERS
*******************
*matrices to store results
estimates drop _all
matrix eff_norm_empl = (.,.,.,.,.,.,.)
matrix colnames eff_norm_empl= "lyrs" "lyr" "dir" "b" "ser" "pv" "Nobs"
matrix eff_wages = (.,.,.,.,.,.,.)
matrix colnames eff_wages = "lyrs" "lyr" "dir" "b" "ser" "pv" "Nobs"
matrix eff_educ = (.,.,.,.,.,.,.)
matrix colnames eff_educ = "lyrs" "lyr" "dir" "b" "ser" "pv" "Nobs"
matrix eff_tenure = (.,.,.,.,.,.,.)
matrix colnames eff_tenure = "lyrs" "lyr" "dir" "b" "ser" "pv" "Nobs"

forvalues b=0/3 {

			forvalues l=0/`b' {
			
				
				count if det_dlog_empl_lyr`l'~=. & det_dlog_Sales~=. & count_mgmt_unt==`b' & count_mgmt_unt==lnext
				if r(N)>5 {
						display " ---------- outcomes lyr `l' -----"
						
						*norm empl
						qui reg det_dlog_norm_empl_lyr`l'  det_dlog_Sales if count_mgmt_unt==`b' & count_mgmt_unt==lnext, r noconst
						matrix eff_norm_empl = (eff_norm_empl \ `b', `l', 1, _b[det_dlog_Sales], _se[det_dlog_Sales], ///
											2*ttail(e(df_r),abs(_b[det_dlog_Sales]/_se[det_dlog_Sales])), e(N))
						estimates store h`b'`l'

						*avg wwage
						qui reg det_dlog_avg_wage_lyr`l'  det_dlog_Sales if count_mgmt_unt==`b'  & count_mgmt_unt==lnext, r noconst
						matrix eff_wages = (eff_wages \ `b', `l', 1, _b[det_dlog_Sales], _se[det_dlog_Sales], ///
											2*ttail(e(df_r),abs(_b[det_dlog_Sales]/_se[det_dlog_Sales])), e(N))
						estimates store w`b'`l'

						*educ
						qui reg det_dlog_avg_educ_lyr`l'  det_dlog_Sales if count_mgmt_unt==`b' & count_mgmt_unt==lnext, r noconst
						matrix eff_educ = (eff_educ \ `b', `l', 1, _b[det_dlog_Sales], _se[det_dlog_Sales], ///
											2*ttail(e(df_r),abs(_b[det_dlog_Sales]/_se[det_dlog_Sales])), e(N))
						estimates store h`b'`l'

						*tenure
						qui reg det_dlog_avg_tenure_lyr`l'  det_dlog_Sales if count_mgmt_unt==`b'  & count_mgmt_unt==lnext, r noconst
						matrix eff_tenure = (eff_tenure \ `b', `l', 1, _b[det_dlog_Sales], _se[det_dlog_Sales], ///
											2*ttail(e(df_r),abs(_b[det_dlog_Sales]/_se[det_dlog_Sales])), e(N))
						estimates store w`b'`l'
					
						
				}
				else {
						matrix eff_norm_empl = (eff_norm_empl \ `b', `l', 1, .,.,.,r(N))
						matrix eff_wages = (eff_wages \ `b', `l', 1, .,.,.,r(N))
				}
				
				
				
			}
		
	
	
}
*


*displaying table A.8: avg. log change norm empl for firms that do not transition
***************************************************************************
matlist eff_norm_empl

*displaying table A.9: avg. log change wages for firms that do not transition
***************************************************************************
matlist eff_wages

*displaying table A.10: knowledge, non-switchers
********************************************************
*experience in establishment
matlist eff_tenure
*education
matlist eff_educ

*************************** end tables



*
*tables A.11, A.12, A.14
***************************
*switchers, no controls
**************************

*matrices to store results
foreach v in avg_wage_lyr norm_empl_lyr avg_educ_lyr avg_tenure_lyr {
		matrix eff_`v' = (.,.,.,.,.,.,.)
		matrix colnames eff_`v' = "bef" "aft" "lyr" "b" "ser" "pv" "Nobs"
	}
*
*regressions
forvalues b=0/3 {
	forvalues a=0/3 {
	
		local mm=min(`a',`b')
		
		if `a'~=`b' {
			display " -------------------------------------------------- from `b' to `a' ----------"
			forvalues l=0/`mm' {
			
				count if det_dlog_norm_empl_lyr`l'~=. & count_mgmt_unt==`b' & lnext==`a'  
				if r(N)>5 {
						display " ---------- outcomes lyr `l' -----"
						
						foreach v in avg_wage_lyr norm_empl_lyr avg_educ_lyr avg_tenure_lyr {
						qui reg det_dlog_`v'`l' if count_mgmt_unt==`b' & lnext==`a'    , r
						matrix eff_`v' = (eff_`v' \ `b', `a', `l', _b[_cons], _se[_cons], ///
											2*ttail(e(df_r),abs(_b[_cons]/_se[_cons])), e(N))
						}
						
				}
				else {
						matrix eff_norm_empl = (eff_norm_empl \ `b', `a', `l', .,.,.,.)
				}
			}
		}
	}
	
}
*
*displaying table A.11: avg. log change norm empl for firms that transition
***************************************************************************
matlist eff_norm_empl_lyr

*displaying table A.12: avg. log change wages for firms that transition
***************************************************************************
matlist eff_avg_wage_lyr


*displaying table A.14: knowledge, switchers
******************************************************
*experience in establishment
matlist eff_avg_tenure_lyr
*education
matlist eff_avg_educ_lyr 

*********************end table
	
		
*table A.13: decomposition of total change in wage
*************************************************
local dw=0.05
local up=99.95
*generating matrices (columns pv0 pv1 will contain pvalues
*for a test of the coefficient being different from zero and
*different from 1)
display "empl in common layers, next"
matrix eff_wages_emplnotop= (.,.,.,.,.,.,.)
matrix colnames eff_wages_emplnotop= "bef" "aft"  "b" "ser" "pv0" "pv1" "Nobs"
display "wages in added layers"
matrix eff_wages_added= (.,.,.,.,.,.,.)
matrix colnames eff_wages_added= "bef" "aft"  "b" "ser" "pv0" "pv1" "Nobs"
display "wages in common layers"
matrix eff_wages_com= (.,.,.,.,.,.,.)
matrix colnames eff_wages_com= "bef" "aft"  "b" "ser" "pv0" "pv1" "Nobs"
display "wages overall"
matrix eff_wages_all= (.,.,.,.,.,.,.)
matrix colnames eff_wages_all= "bef" "aft"  "b" "ser" "pv0" "pv1" "Nobs"
*	

*generating detrended avg wage in the firm
capture drop det_dlog_avg_wage
gen det_dlog_avg_wage=.
forvalues y=1998/2010 {
	sum dlog_avg_wage if jahr==`y'
	replace det_dlog_avg_wage=dlog_avg_wage-r(mean) if jahr==`y'
}
gen kwage=exp(dlog_avg_wage-det_dlog_avg_wage)

*regressions
forvalues b=0/2 {
		local in=`b'+1
		forvalues a=`in'/3 {
				
			*transition considered
			gen in_sample = count_mgmt_unt==`b' & lnext==`a'
			count if in_sample==1 
			if r(N)>=5 {
						
						
						display "--------------------`b', `a'"

						*empl in common layers
						_pctile sharehours_notop_next  if in_sample==1, p(0.05 99.95)
						reg sharehours_notop_next if in_sample==1 & ///
							sharehours_notop_next>=`r(r1)' & sharehours_notop_next<=`r(r2)', r
						local pv0=2*ttail(e(df_r),abs(_b[_cons]/_se[_cons]))
						quietly lincom _b[_cons]-1			
						local pv1=2*ttail(e(df_r),abs(r(estimate)/r(se)))
						matrix eff_wages_emplnotop = (eff_wages_emplnotop \ `b', `a', _b[_cons], _se[_cons], ///
								`pv0', `pv1', e(N))									

						*new layers
						gen temp=added/kwage
						_pctile temp  if in_sample==1, p(0.05 99.95)
						reg temp if in_sample==1  & temp>=`r(r1)' & temp<=`r(r2)', r
						local pv0=2*ttail(e(df_r),abs(_b[_cons]/_se[_cons]))
						quietly lincom _b[_cons]-1				
						local pv1=2*ttail(e(df_r),abs(r(estimate)/r(se)))
						matrix eff_wages_added = (eff_wages_added \ `b', `a', _b[_cons], _se[_cons], ///
								`pv0', `pv1', e(N))
						drop temp

						*common layers
						gen temp=common/kwage
						_pctile temp  if in_sample==1, p(0.05 99.95)
						reg temp if in_sample==1  & temp>=`r(r1)' & temp<=`r(r2)', r
						local pv0=2*ttail(e(df_r),abs(_b[_cons]/_se[_cons]))
						quietly lincom _b[_cons]-1				
						local pv1=2*ttail(e(df_r),abs(r(estimate)/r(se)))
						matrix eff_wages_com = (eff_wages_com \ `b', `a', _b[_cons], _se[_cons], ///
								`pv0', `pv1', e(N))
						drop temp
						
						*total change in wage
						_pctile det_dlog_avg_wage  if in_sample==1, p(0.05 99.95)
						reg det_dlog_avg_wage if in_sample==1  & det_dlog_avg_wage>=`r(r1)' & det_dlog_avg_wage<=`r(r2)', r
						local pv0=2*ttail(e(df_r),abs(_b[_cons]/_se[_cons]))
						quietly lincom _b[_cons]-1				
						local pv1=2*ttail(e(df_r),abs(r(estimate)/r(se)))
						matrix eff_wages_all = (eff_wages_all \ `b', `a', _b[_cons], _se[_cons], ///
								`pv0', `pv1', e(N))
				
			} // end if r(N)>=5
			
			drop in_sample 	
			
		} // end forvalues a
			
} // end forvalues b
*top left panel
display "wages in common layers"
matlist eff_wages_com	
*top right panel
display "wages in added layers"
matlist eff_wages_added
*bottom left panel					
display "empl in common layers, next"
matlist eff_wages_emplnotop
*bottom right panel
display "wages overall"
matlist eff_wages_all
		
*************** end table		

log close

clear all
